/*-----------------------------------------------------------------------------
 DiscountCalrActivity - Calculates discounts including double ones. 
 Copyright (C) 2011 z0ltrix aka Amit Parmar

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
-----------------------------------------------------------------------------*/

package com.z0ltrix.DiscountCalcr;

import java.util.Currency;
import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class DiscountCalcrActivity extends Activity {

	// -------------------------------------------------------------------------------------------
	EditText mAmountText;
	SeekBar mDiscountPercentBar;
	TextView mDisplayAmountWithDisc;
	TextView mDisplayDiscSeeked;
	Button mDoubleDButton;
	Button mResetAllButton;
	float vAmountOriginal;
	float vAmountDiscounted;
	float vSavedAmount;
	static int DISCOUNT  = 0;
	static int DISCOUNT2 = 0;
	final static int RETURNCODEDISC = 1010;
	static String CURRENCY_SYMBOL;

	// -------------------------------------------------------------------------------------------

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// ------------------------------------------------------------------------------------
		mAmountText = (EditText) findViewById(R.id.editText1);
		mDiscountPercentBar = (SeekBar) findViewById(R.id.seekBar1);
		mDisplayAmountWithDisc = (TextView) findViewById(R.id.discountResultsText);
		mDisplayDiscSeeked = (TextView) findViewById(R.id.seekBarText);
		mDoubleDButton = (Button) findViewById(R.id.DoubleDiscButton);
		mResetAllButton = (Button) findViewById(R.id.resetButtonid);
		Locale locale = Locale.getDefault();		
		Currency currency = Currency.getInstance(locale);
		CURRENCY_SYMBOL= currency.getSymbol();
		// ------------------------------------------------------------------------------------

		mDiscountPercentBar
				.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

					public void onStopTrackingTouch(SeekBar seekBar) {
						// TODO Auto-generated method stub

					}

					public void onStartTrackingTouch(SeekBar seekBar) {
						// TODO Auto-generated method stub

					}

					public void onProgressChanged(SeekBar seekBar,
							int progress, boolean fromUser) {
						DISCOUNT = progress;
						CalculateDiscount(DISCOUNT, DISCOUNT2);

					}
				});

		mAmountText.addTextChangedListener(new TextWatcher() {

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				CalculateDiscount(DISCOUNT, DISCOUNT2);

			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			public void afterTextChanged(Editable s) {

			}
		});

		mDoubleDButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				Intent intent = new Intent(getApplicationContext(),
						DoubleDiscActivity.class);
				intent.putExtra("DISCOUNT", DISCOUNT2);
				startActivityForResult(intent, RETURNCODEDISC);

			}
		});

		mResetAllButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				DISCOUNT = 0;
				DISCOUNT2 = 0;
				mAmountText.setText("");
				mDiscountPercentBar.setProgress(0);
				CalculateDiscount(DISCOUNT, DISCOUNT2);

			}
		});

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (requestCode == RETURNCODEDISC && resultCode == RESULT_OK) {
			DISCOUNT2 = data.getIntExtra("discountValue", 0);

			CalculateDiscount(DISCOUNT, DISCOUNT2);

		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	protected void CalculateDiscount(int discount1, int discount2) {
		try {
			if (discount2 != 0)
				mDisplayDiscSeeked.setText("Discount: " + discount1 + "% + "
						+ discount2 + "%");
			else
				mDisplayDiscSeeked.setText("Discount: " + discount1 + "%");

			vAmountOriginal = Float.valueOf(mAmountText.getText().toString()) * 100;
			vAmountDiscounted = CalulateDiscountAmount(vAmountOriginal,
					discount1);

			if (discount2 != 0) {

				vAmountDiscounted = CalulateDiscountAmount(vAmountDiscounted,
						discount2);

			}

			vSavedAmount = Math.round(vAmountOriginal - vAmountDiscounted);
			vAmountDiscounted /= 100;
			vSavedAmount /= 100;
			mDisplayAmountWithDisc.setText(getString(
					R.string.mDisplayAmountTextFinal,
					CURRENCY_SYMBOL,
					String.valueOf(vAmountDiscounted),
					CURRENCY_SYMBOL,
					String.valueOf(vSavedAmount)));

		} catch (NumberFormatException NUEX) {
			mDisplayAmountWithDisc.setText(getString(R.string.mDisplayAmountErrorText));
		}

	}

	private float CalulateDiscountAmount(float vAmountOriginal2, int discount) {
		return Math
				.round((vAmountOriginal2 - ((vAmountOriginal2 * discount)) / 100));
	}
}